/*
 * 2006-1-11 lxm 创建。 
 */
package com.hd123.rumba.sql;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.FormatSettings;

/**
 * 数据集 | 数据交换类
 * 
 * @author lxm
 * 
 */
public class DDatasetPage extends DResultPage {

  /** 列定义 */
  private DDatasetColumnDef[] columnDefs;
  /**
   * 取值<br>
   * 二维数组，第一维表示记录索引，第二维表示字段索引。
   */
  private String[][] values;

  private static String VALUE_NULL = "__NULL__";

  public void assignFrom(List list) throws BusinessException, HibernateException {
    super.assignFrom(list);

    for (int i = 0; i < list.size(); i++) {
      Object[] record = null;
      if (list.get(i) instanceof Object[])
        record = (Object[]) list.get(i);
      else {
        record = new Object[1];
        record[0] = list.get(i);
      }
      if (i == 0) {
        values = new String[list.size()][record.length];
        columnDefs = new DDatasetColumnDef[record.length];
      }
      for (int j = 0; j < record.length; j++) {
        if (columnDefs[j] == null)
          columnDefs[j] = new DDatasetColumnDef();
        if (record[j] == null) {
          values[i][j] = VALUE_NULL;
          continue;
        }
        if (record[j] instanceof String) {
          columnDefs[j].setDataType(DDatasetColumnDef.VALUE_DATATYPE_STRING);
          String value = (String) record[j];
          values[i][j] = value;
        } else if (record[j] instanceof Integer) {
          columnDefs[j].setDataType(DDatasetColumnDef.VALUE_DATATYPE_INTEGER);
          Integer value = (Integer) record[j];
          values[i][j] = value.toString();
        } else if (record[j] instanceof Long) {
          columnDefs[j].setDataType(DDatasetColumnDef.VALUE_DATATYPE_LONG);
          Long value = (Long) record[j];
          values[i][j] = value.toString();          
        } else if (record[j] instanceof Date) {
          columnDefs[j].setDataType(DDatasetColumnDef.VALUE_DATATYPE_DATETIME);
          Date value = (Date) record[j];
          values[i][j] = FormatSettings.formatDatetime(FormatSettings.SDFyMdHms, value);
        } else if (record[j] instanceof BigDecimal) {
          columnDefs[j].setDataType(DDatasetColumnDef.VALUE_DATATYPE_CURRENCY);
          BigDecimal value = (BigDecimal) record[j];
          values[i][j] = value.toString();
        } else if (record[j] instanceof Boolean) {
          columnDefs[j].setDataType(DDatasetColumnDef.VALUE_DATATYPE_BOOLEAN);
          Boolean value = (Boolean) record[j];
          values[i][j] = value.toString();
        } else
          throw new BusinessException("不能解析的数据类型:" + record[j].getClass().toString());
      }
    }
  }

  public DDatasetColumnDef[] getColumnDefs() {
    return columnDefs;
  }

  public void setColumnDefs(DDatasetColumnDef[] columnDefs) {
    this.columnDefs = columnDefs;
  }

  public String[][] getValues() {
    return values;
  }

  public void setValues(String[][] values) {
    this.values = values;
  }

}
